package recallin.activities;

import recallin.database.ServerConnection;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

/**
 * Handles all of the menus in the application
 * 
 * @author philipmasek
 * 
 */

public class MenuActivity extends TabActivity {
	private TabHost mTabHost;
	public static int screen_width;
	public static int screen_height;
	public static ServerConnection connection = LoginActivity.connection;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu);
		screen_width = getWindowManager().getDefaultDisplay().getWidth();
		screen_height = getWindowManager().getDefaultDisplay().getHeight();

		mTabHost = getTabHost();
		setupTab(new TextView(this), "", R.drawable.btn_settings,
				SettingsActivity.class);
		setupTab(new TextView(this), "", R.drawable.btn_places,
				PlacesActivity.class);
		setupTab(new TextView(this), " ", R.drawable.btn_menu, null);
		setupTab(new TextView(this), "", R.drawable.btn_friends,
				FriendsActivity.class);
		setupTab(new TextView(this), "", R.drawable.btn_navi,
				NavigationActivity.class);

		mTabHost.setCurrentTab(getIntent().getIntExtra("tab_index", 0));

		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals(" ")) {
					finish();
				}
			}
		});

	}

	private void setupTab(final View view, final String tag, int resid,
			Class cls) {
		View tabview = createTabView(mTabHost.getContext(), tag, resid);
		TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
				.setContent(new TabContentFactory() {
					public View createTabContent(String tag) {
						return view;
					}
				});
		if (cls != null) {
			if (cls != null) {
				Intent intent = new Intent(getApplicationContext(), cls);
				setContent.setContent(intent);
			}

		}

		mTabHost.addTab(setContent);
	}

	private static View createTabView(final Context context, final String text,
			int resid) {
		View view = LayoutInflater.from(context)
				.inflate(R.layout.tabs_bg, null);
		view.setLayoutParams(new LayoutParams(screen_width / 5,
				(int) (screen_width / 6.5)));
		view.setBackgroundResource(resid);
		TextView tv = (TextView) view.findViewById(R.id.tabsText);
		tv.setText(text);
		return view;
	}

	public static void hideKeyboard(View v, Context context) {
		InputMethodManager mgr = (InputMethodManager) context
				.getSystemService(context.INPUT_METHOD_SERVICE);
		mgr.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}

	public static void showKeyboard(View v, Context context) {
		InputMethodManager mgr = (InputMethodManager) context
				.getSystemService(context.INPUT_METHOD_SERVICE);
		mgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
	}

	public static void setupSearchField(final Context context,
			final EditText editText) {
		editText.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean focused) {
				if (focused) {
					showKeyboard(v, context);
				} else
					hideKeyboard(v, context);
			}
		});
	}

	private class CloseActivity extends Activity {
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			finish();
			super.onCreate(savedInstanceState);
		}
	}

}